package com.springBoot.util;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import lombok.extern.slf4j.Slf4j;
/**
 * 
 * @说明AOP拦截器<br/>
 * @作者徐晓辉<br/>
 * @日期2018年11月8日<br/>
 */
@Aspect
@Component
@Slf4j
public class AOP_HandlerInterceptor {
	@Pointcut("execution(public * com.springBoot.controller.*.*(..))")
	public void webLog() {
	}
	
	/**	前置通知*/
	@Before("webLog()")
	public void doBefore(JoinPoint joinPoint) throws Throwable {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		log.info("URL请求地址 : " + request.getRequestURL().toString());
		log.info("HTTP_METHOD请求方式 : " + request.getMethod());
		log.info("IP地址 : " + request.getRemoteAddr());
		Enumeration<String> enu = request.getParameterNames();
		while (enu.hasMoreElements()) {
			String name = (String) enu.nextElement();
			log.info("参数名:{},参数值:{}", name, request.getParameter(name));
		}
	}
	/**	后置通知*/
	@AfterReturning(returning = "ret", pointcut = "webLog()")
	public void doAfterReturning(Object ret) throws Throwable {
		// 处理完请求，返回内容
		log.info("RESPONSE相应结果 : " + ret);
	}
}
